package iedcyt.acute.dataAccess.dao.endnote;

import iedcyt.acute.dataAccess.domain.endnote.Ma0Bis;
import iedcyt.acute.dataAccess.domain.maauco.Ma0;
import iedcyt.acute.dataAccess.generic.SpringDao;
import iedcyt.acute.dataAccess.idao.endnote.IMa0BisDao;

import java.util.List;

public class Ma0BisDao extends SpringDao implements IMa0BisDao {

	@Override
	public void create() {
		//comprobamos si la tabla existe y si no la creamos
		try {
			_jdbcTemplate.queryForInt("select count(*) from Ma0Bis");
		} catch (Exception e) {
			_jdbcTemplate.execute("CREATE TABLE Ma0Bis (RECID varchar(50), UTID varchar(50), JOURNAL2 varchar(255), ABBREV varchar(250),ABBREV22 varchar(250), SE varchar(250), PD varchar(250), IS varchar(250), SI varchar(250),	NPAG varchar(250), DI varchar(250), ISSN varchar(250), PU varchar(250), PA varchar(250),PI varchar(250))");
		}
	}
	
	@Override
	public List<Ma0Bis> selectAll() {
		return _jdbcTemplate.query("SELECT *  FROM Ma0Bis",
				new Ma0BisRowMapper());
	}

	@Override
	public List<Ma0Bis> selectByRange(int RECID1, int RECID2) {
		return _jdbcTemplate.query("SELECT *  FROM Ma0Bis WHERE RECID >= ? AND RECID <= ?",
				new Object[] { RECID1, RECID2 },
				new Ma0BisRowMapper());
		
	}

	@Override
	public int selectMaxRECID() {
		return _jdbcTemplate.queryForInt("SELECT MAX(RECID) FROM Ma0Bis");
	}

	@Override
	public int selectMinRECID() {
		return _jdbcTemplate.queryForInt("SELECT MIN(RECID) FROM Ma0Bis");
	}

	@Override
	public List<Ma0Bis> selectByUTID(String uTID) {
		return _jdbcTemplate.query("SELECT *  FROM Ma0Bis WHERE UTID = ?",
				new Object[] { uTID },
				new Ma0BisRowMapper());
	}
	
	@Override
	public void insert(Ma0Bis ma0b) {
		
		_jdbcTemplate.update("INSERT INTO Ma0Bis (RECID, UTID, JOURNAL2, ABBREV,ABBREV22, SE, PD, IS, SI,	NPAG, DI, ISSN, PU, PA,	PI) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
				new Object[] { ma0b.get_RECID(), ma0b.get_UTID(), ma0b.get_JOURNAL2(), ma0b.get_ABBREV(),
				ma0b.get_ABBREV22(), ma0b.get_SE(), ma0b.get_PD(), ma0b.get_IS(), ma0b.get_SI(),
				ma0b.get_NPAG(), ma0b.get_DI(), ma0b.get_ISSN(), ma0b.get_PU(), ma0b.get_PA(),
				ma0b.get_PI()});
		
	}

}
